import {
  createSlice,

} from "@reduxjs/toolkit";

export const listSlice = createSlice({
  name: "list",
  // `createSlice` 将从 `initialState` 参数推断 state 类型
  initialState: {
    data: []
  },
  reducers: {
    // 获取数据 就业态势
    set_EmpList(state, {
      type,
      payload
    }) {
      state.data = payload
    },
    // 智能检测数据
    set_IntList(state, {
      type,
      payload
    }) {
      state.data = payload
    },
    // 安防管理数据
    set_SecList(state, {
      type,
      payload
    }) {
      state.data = payload
    },
    // 设备运维
    set_EqiList(state, {
      type,
      payload
    }) {
      state.data = payload
    },
    // 删除
    del_Emp(state, {
      type,
      payload
    }) {
      console.log('payload', payload);
      state.data.forEach((item, index) => {
        if (item.stuNo == payload) {
          state.data.splice(index, 1)
        }
      })
    },
    // 批量删除
    del_EmpAll(state, {
      type,
      payload
    }) {
      state.data = state.data.filter(value => {
        return !payload.includes(value.id)
      })
      console.log(state.data, '全删');
    },
    // 批量删除stuNo
    del_EmpAllStu(state, {
      type,
      payload
    }) {
      state.data = state.data.filter(value => {
        return !payload.includes(value.stuNo)
      })
      console.log(state.data, '全删stuNo');
    },
    // 添加
    add_Emp(state, {
      type,
      payload
    }) {
      state.data.unshift(payload)
    },
    // 编辑
    edit_Emp(state, {
      type,
      payload
    }) {
      state.data.forEach((item, index) => {
        if (item.key === payload.key) {
          state.data[0] = payload
        }
      })
    }
  },
});

export const {
  set_EmpList,
  del_Emp,
  add_Emp,
  edit_Emp,
  del_EmpAll,
  set_IntList,
  set_SecList,
  set_EqiList,
  del_EmpAllStu
} = listSlice.actions;

export default listSlice.reducer;